//
//  DBEmptyViewTableViewController.m
//  DuoBao
//
//  Created by arons on 16/3/28.
//  Copyright © 2016年 aron. All rights reserved.
//

#import "DBEmptyViewTableViewController.h"
#import "BusinessInterface.h"
#import "MBProgressHUD+MJ.h"
#import "UIViewAdditions.h"

@interface DBEmptyViewTableViewController ()

@property (nonatomic, strong) UIView* emptyView;
@property (nonatomic, weak) UIImageView* emptyImageView;
@property (nonatomic, weak) UILabel* emptyMsglabel;
@property (nonatomic, weak) UIButton* actionBtn;

@property (nonatomic, strong) UIImage* emptyImage;
@property (nonatomic, copy) NSString* emptyMsg;
@property (nonatomic, copy) NSString* actionBtnTitle;
@property (nonatomic, strong) UIView* additionView;

@end

@implementation DBEmptyViewTableViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        _shouldAddAdditionView = YES;
        _shouldUseDefaultHint = YES;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.emptyViewTableViewDataSource = self;
    self.emptyViewTableViewDelegate = self;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    BMLog(@"====");
    self.emptyViewTableViewDataSource = nil;
    self.emptyViewTableViewDelegate = nil;
}


#pragma mark - ......::::::: override :::::::......
/**
 *  处理加载结束数据回调
 *  @param tmpDatas 某一次请求的数据
 */
- (void)handleDataLoadFailSuccess:(NSArray*)tmpDatas refresh:(BOOL)refresh{
    
    // 先调用父类进行处理
    [super handleDataLoadFailSuccess:tmpDatas refresh:refresh];
    
    // 设置空列表视图
    [self updateEmptyView];
    
    // 处理上下拉
    [self.tableView.mj_header endRefreshing];
    if (tmpDatas.count < GdefaultPageSize) {
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    }else {
        [self.tableView.mj_footer endRefreshing];
    }
    [MBProgressHUD hideHUDForView:self.view];
    
    // 刷新显示
    [self.tableView reloadData];
}

- (void)handleDataLoadFail {
    
    // 先调用父类进行处理
    [super handleDataLoadFail];
    
    // 设置空列表视图
    [self updateEmptyView];
}

#pragma mark - init f

static CGFloat emptyViewH = 310;

- (UIView *)emptyView {
    if (!_emptyView) {
        
        CGFloat PADDINV = 8;
        
        UIView* emptyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, emptyViewH)];
        _emptyView = emptyView;
        
        CGFloat emptyTopViewH = 310;
        UIView* emptyTopView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, emptyTopViewH)];
        emptyTopView.backgroundColor = [UIColor whiteColor];
        [emptyView addSubview:emptyTopView];
        
        CGFloat emptyImageViewW = 180;
        CGFloat emptyImageViewH = 160;
        CGFloat emptyImageViewX = (SCREEN_WIDTH - emptyImageViewW)/2;
        UIImageView* emptyImageView = [UIImageView new];
        emptyImageView.frame = CGRectMake(emptyImageViewX, PADDINV*2, emptyImageViewW, emptyImageViewH);
        emptyImageView.contentMode = UIViewContentModeScaleAspectFit;
        emptyImageView.clipsToBounds = YES;
        emptyImageView.layer.cornerRadius = 0;
        emptyImageView.layer.masksToBounds = YES;
        emptyImageView.image = [UIImage imageNamed:@"ic_empty_list"];
        [emptyTopView addSubview:emptyImageView];
        _emptyImageView = emptyImageView;
        
        UILabel* emptyMsglabel = [UILabel new];
        emptyMsglabel.frame = CGRectMake(0, CGRectGetMaxY(emptyImageView.frame)+PADDINV, SCREEN_WIDTH, 20);
        emptyMsglabel.font = [UIFont systemFontOfSize:16];
        emptyMsglabel.textAlignment = NSTextAlignmentCenter;
        emptyMsglabel.textColor = [UIColor themeGrayColorWithHint];
        emptyMsglabel.backgroundColor = [UIColor clearColor];
        emptyMsglabel.text = @"你的清单空空如也";
        [emptyTopView addSubview:emptyMsglabel];
        _emptyMsglabel = emptyMsglabel;
        
        CGFloat actionBtnW = 160;
        CGFloat actionBtnH = 40;
        CGFloat actionBtnX = (SCREEN_WIDTH - actionBtnW)/2;
        CGFloat actionBtnY = CGRectGetMaxY(emptyMsglabel.frame)+PADDINV;
        UIButton* actionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        actionBtn.frame = CGRectMake(actionBtnX, actionBtnY, actionBtnW, actionBtnH);
        actionBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [actionBtn setTitle:@"立即夺宝" forState:UIControlStateNormal];
        [actionBtn setTitleColor:[UIColor themeHighLightColor] forState:UIControlStateNormal];
        [actionBtn setTitleColor:[UIColor themeHighLightColorH] forState:UIControlStateHighlighted];
        actionBtn.layer.cornerRadius = 5;
        actionBtn.layer.masksToBounds = YES;
        actionBtn.layer.borderColor = [UIColor themeHighLightColorH].CGColor;
        actionBtn.layer.borderWidth = 1.0f;
        [actionBtn addTarget:self action:@selector(onDBEmptyViewActionBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [emptyTopView addSubview:actionBtn];
        _actionBtn = actionBtn;
    }
    return _emptyView;
}

static int ADDITION_VIEW_TAG = 16233;

- (void)updateEmptyView {
    
    // 设置显示上拉加载更多
    if (self.objects.count <= 0) {
        // 设置不显示上拉加载
        self.tableView.mj_footer = nil;
        
        // 设置空视图的内容显示
        // 从用户自定义数据代理中取数据，设置空列表视图
        if (_emptyViewTableViewDataSource) {
            if ([_emptyViewTableViewDataSource respondsToSelector:@selector(imageForEmpty)]) {
                UIImage* tmpEmptyImage = [_emptyViewTableViewDataSource imageForEmpty];
                _emptyImage = tmpEmptyImage?tmpEmptyImage:nil;
            }
            
            if ([_emptyViewTableViewDataSource respondsToSelector:@selector(titleForEmptyActionButton)]) {
                NSString* tmpActionBtnTitle = [_emptyViewTableViewDataSource titleForEmptyActionButton];
                _actionBtnTitle = tmpActionBtnTitle?tmpActionBtnTitle:nil;
            }
            
            if ([_emptyViewTableViewDataSource respondsToSelector:@selector(messageForEmpty)]) {
                NSString* tmpEmptyMsg = [_emptyViewTableViewDataSource messageForEmpty];
                _emptyMsg = tmpEmptyMsg?tmpEmptyMsg:nil;;
            }
            
            if ([_emptyViewTableViewDataSource respondsToSelector:@selector(additionViewForEmpty)]) {
                UIView* additionView = [_emptyViewTableViewDataSource additionViewForEmpty];
                _additionView = additionView;
            }
        }
        
        // 添加视图到TableView上
        [self.tableView addSubview:[self emptyView]];

        // 处理默认显示数据，用户没有提供代理方法传递数据，在设置了使用默认的提示文字清空设置空视图显示的文字
        if (_shouldUseDefaultHint) {
            if(!_emptyImage) _emptyImage = [UIImage imageNamed:@"img_net_work_error"];
            if(!_emptyMsg) _emptyMsg = @"我们都有不顺利的时候";
            if(!_actionBtnTitle) _actionBtnTitle = @"去其他地方";
        }
        
        // 图片
        if (_emptyImage) {
            _emptyImageView.image = _emptyImage;
        }else{
            _emptyImageView.hidden = YES;
        }
        
        // 提示信息
        if (_emptyMsg) {
            _emptyMsglabel.text = _emptyMsg;
        }else{
            _emptyMsglabel.hidden = YES;
        }
        
        // 按钮
        if (_actionBtnTitle) {
            [_actionBtn setTitle:_actionBtnTitle forState:UIControlStateNormal];
        }else{
            _actionBtn.hidden = YES;
        }
        
        // 附加的视图
        if (_additionView) {
            
            UIView* oldAdditionView = [[self emptyView] viewWithTag:ADDITION_VIEW_TAG];
            if (oldAdditionView) {
                [oldAdditionView removeFromSuperview];
                [self emptyView].height = emptyViewH;
            }
            
            _additionView.tag = ADDITION_VIEW_TAG;
            _additionView.y = emptyViewH;
            
            CGFloat additionViewH = _additionView.height;
            [self emptyView].height = emptyViewH + additionViewH;
            
            [self emptyView].alpha = 1.0f;
            [[self emptyView] addSubview:_additionView];
        }
        
    }else {
        // 设置支持下拉刷新
        [self setupPullUp];
        
        [UIView animateWithDuration:0.14f animations:^{
            [self emptyView].alpha = 0.1f;
            
        } completion:^(BOOL finished) {
            // 设置不显示空视图
            [[self emptyView] removeFromSuperview];
        }];
    }

}

- (void)onDBEmptyViewActionBtnClick:(UIButton*)sender {
    if (_emptyViewTableViewDelegate
        && [_emptyViewTableViewDelegate respondsToSelector:@selector(emptyActionButtonClick)]) {
        [_emptyViewTableViewDelegate emptyActionButtonClick];
    }
}

@end
